package com.roy;

/**
 * @author xiaoxiaokui
 * @description 有一个OA系统，每个月需要定时的计算大家的工资
 * 有一个程序员，想要偷偷修改工资的计算方法，偷偷加工资
 * @date 2021/11/21
 */
public class OADemo {

    public static void main(String[] args) throws Exception {
        Double salary = 2000.00;
        Double money;
//        URL jarPath = new URL("file:E:\\classLoaderStory\\SalaryCaler.jar");
//        URLClassLoader urlClassLoader = new URLClassLoader(new URL[]{jarPath});
        SalaryClassLoader salaryClassLoader = new SalaryClassLoader("E:\\SalaryCaler\\out\\production\\SalaryCaler\\");
        while (true) {
//            money = calSalary(salary, urlClassLoader);
            money = calSalary(salary, salaryClassLoader);
            System.out.println("实际到手的工资：" + money);
            Thread.sleep(1000);
        }
    }

    private static Double calSalary(Double salary, ClassLoader classLoader) throws Exception {
//        SalaryCaler salaryCaler = new SalaryCaler();
//        return salaryCaler.cal(salary);
        Class<?> clazz = classLoader.loadClass("com.roy.SalaryCaler");
        Object object = clazz.newInstance();
        return (Double) clazz.getMethod("cal", Double.class).invoke(object, salary);
    }
}
